package com.eastfair.venuebooking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.venuebooking.dao.UserCustomerStarMapper;
import com.eastfair.venuebooking.dto.UserCustomerStarDTO;
import com.eastfair.venuebooking.dto.UserCustomerStarQuery;
import com.eastfair.venuebooking.entity.UserCustomerStar;
import com.eastfair.venuebooking.service.UserCustomerStarService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collection;

/**
 * <p>
 * 业务实现类
 * 客户重点关注表
 * </p>
 *
 * @author linan
 * @date 2022-06-08
 */
@Slf4j
@Service
public class UserCustomerStarServiceImpl extends SuperServiceImpl<UserCustomerStarMapper, UserCustomerStar> implements UserCustomerStarService {


    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<UserCustomerStar> modelList) {
        modelList.forEach(s -> s.setIsDeleted(BusinessConstant.YES));
        // TODO: 2021/10/29 这里需要操作其他业务，如清空角色等
        return R.successDef();
    }

    @Override
    protected R<Boolean> handlerSave(UserCustomerStar model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setTenantId(ContextUtil.getTenantId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        return R.successDef();
    }

    @Override
    public boolean existUserCustomerStar(UserCustomerStarQuery query) {
        QueryWrapper<UserCustomerStar> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(query.getUserId() != null, UserCustomerStar::getUserId, query.getUserId())
                .eq(query.getCustomerId() != null,
                        UserCustomerStar::getCustomerId, query.getCustomerId());
        UserCustomerStar userCustomerStar = getOne(queryWrapper);
        return userCustomerStar != null;
    }

    @Override
    public boolean removeByUserIdAndCustomerId(UserCustomerStarDTO updateDTO) {

        QueryWrapper<UserCustomerStar> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(updateDTO.getUserId() != null, UserCustomerStar::getUserId, updateDTO.getUserId())
                .eq(updateDTO.getCustomerId() != null,
                        UserCustomerStar::getCustomerId, updateDTO.getCustomerId());
        return remove(queryWrapper);
    }
}
